<?php
/**
 * 商家（开通有赞） - 有赞订单同步
 *
 * Author: hezhizheng
 * Date: 2018/7/16
 * Time: 16:01
 * Created by Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\Youzan\SyncOrdersRequest;
use App\Http\Requests\Admin\OrderIndexRequest;
use App\Models\Customer\Customer;
use App\Models\Order;
use App\Services\Utils\Guard;
use App\Services\Youzan\Mock;
use App\Services\Youzan\OrderRepository;
use App\Services\Youzan\OrderService;
use Illuminate\Http\Request;

class OrderController extends ApiController
{
    const ROUTES = [
      'sync' => 'api.admin.customer.order.sync',
      'index' => 'api.admin.customer.order.index',
      'show' => 'api.admin.customer.order.show',
    ];

    protected $orderService;
    protected $orderRepository;

    public function __construct(OrderService $orderService, OrderRepository $orderRepository)
    {
        $this->orderService = $orderService;
        $this->orderRepository = $orderRepository;
    }


    /**
     * 同步订单到orders表.
     *
     * @param SyncOrdersRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Throwable
     */
    public function syncOrders(SyncOrdersRequest $request)
    {
        $this->checkCustomer();

        $request->checkTimeSpan();

        /** @var Customer $customer */
        $customer = Guard::customer();

        $youzanAuthorizer = $customer->youzanAuthorizer;
            $this->checkIsTrue($youzanAuthorizer,trans('admin.no_auth_to_youzan'));

        $trades_sold_params = $this->orderRepository->assembleTradesSoldGetParams(
            1, 100,
            $request->start_created, $request->end_created
        );

        /**
         * 获取某一时间段(默认过去一周)的所有订单
         *
         * @see Mock::queryOrders()
         */
        $list = $this->orderRepository->getAllTradesSold($youzanAuthorizer, $trades_sold_params);

        // 格式化数据
        $transformList = $this->orderService->transformOrderLists($list);

        // 同步订单到orders表
        $sync = $this->orderRepository->syncOrders($transformList);

        return $this->response(compact('sync'));
    }

    /**
     * 后台客户 查看有赞订单列表
     * @test
     *
     * @param OrderIndexRequest $request
     *
     * @throws \Exception
     * @return array|\Illuminate\Http\Response
     */
    public function index(OrderIndexRequest $request)
    {
        $this->checkCustomer();

        /** @var Customer $customer */
        $customer = Guard::customer();
            $this->checkIsTrue($customer->youzan_authorizer_id, trans('admin.youzan-shop'));

        $stores = $customer->stores()->get(['id', 'name']);

        $orders = $this->orderRepository->getOrdersForStoreIds($customer, $request);

        return $this->response(compact('orders', 'stores'));
    }

    /**
     * 查询当个订单的详情
     *
     * @param $trad_no
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     * @throws \Throwable
     */
    public function show(string $trad_no)
    {
        $this->checkCustomer();

        /** @var Customer $customer */
        $customer = Guard::customer();

        $youzanAuthorizer = $customer->youzanAuthorizer;
            $this->checkIsTrue($youzanAuthorizer,trans('admin.no_auth_to_youzan'));

        $order = (new Order)->where('trad_no', $trad_no)->first();
            $this->checkItemExist($order);

        /**
         * @see Mock::queryOrderDetail()
         */
        $order_info = $this->orderRepository->getTrade($youzanAuthorizer, ['tid' => $trad_no]);

        /** 处理订单退款的状态 */
        $order_info = $this->orderRepository->transformOrderRefundInfo($order_info);

        return $this->response(compact('order_info'));
    }
}